package com.sc.test.utils;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Shen Chao on 2018-01-13-013 下午 15:07.
 */

public class MyViewGroup extends ViewGroup {
    public MyViewGroup(Context context) {
        super(context);
    }

    public MyViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //对所有子view测量
        measureChildren(widthMeasureSpec, heightMeasureSpec);

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int childConut = getChildCount();
        if (childConut == 0) {
            //没有子view，viewgroup没有存在意思，设为0
            setMeasuredDimension(0, 0);
        } else {
            //如果都是包裹内容
            if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) {
                //高度设为所有ziview高度相加，宽度设为ziview最大宽度
                int width = getMaxChildWidth();
                int height = getTotleHeight();
                setMeasuredDimension(width, height);
            } else if (heightMode == MeasureSpec.AT_MOST) {
                //如果只有高度是包裹内容，高度为和，宽度为viewgroup自己测量的宽度
                setMeasuredDimension(widthSize, getTotleHeight());
            } else if (widthMode == MeasureSpec.AT_MOST) {
                //如果宽度为包裹内容，高度为测量值，宽度为子view最大宽度
                setMeasuredDimension(getMaxChildWidth(), heightSize);
            }
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int count = getChildCount();
        //记录当前高度位置
        int curHeight = t;
        //将view逐个摆放
        for (int i = 0; i < count; i++) {
            View childView = getChildAt(i);
            int width = childView.getMeasuredWidth();
            int height = childView.getMeasuredHeight();
            childView.layout(l, curHeight, l + width, curHeight + height);
            curHeight += height;
        }
    }

    /**
     * 获取所有子view高度和
     *
     * @return
     */
    private int getTotleHeight() {
        int childCount = getChildCount();
        int sumHeight = 0;
        for (int i = 0; i < childCount; i++) {
            View childView = getChildAt(i);
            sumHeight += childView.getMeasuredHeight();
        }
        return sumHeight;
    }

    /**
     * 获取ziview最大宽度
     *
     * @return
     */
    private int getMaxChildWidth() {
        int childCount = getChildCount();
        int maxWidth = 0;
        for (int i = 0; i < childCount; i++) {
            View childView = getChildAt(i);
            if (childView.getMeasuredWidth() > maxWidth) {
                maxWidth = childView.getMeasuredWidth();
            }
        }
        return maxWidth;
    }

}
